#include <iostream>

using namespace std;
// #define ONLINE_GUDGE
using PII = pair<int, int>;
#define x first 
#define y second 
const int N = 1010, M = N*N;

int n,m;
char g[N][N];
PII q[M];
bool st[N][N]; // 判重 state

void bfs(int sx, int sy)
{
    int hh = 0, tt = 0; // 数组模拟队列
    q[0] = {sx, sy};
    st[sx][sy] = true;
    
    while(hh <= tt)
    {
        PII t = q[hh++];
        for(int i = t.x - 1; i <= t.x+1; i++)
            for(int j = t.y - 1; j <= t.y+1; j++)
            {
                if(i == t.x && j == t.y)    continue;
                if(i < 0 || i >= n || j < 0 || j >= m || g[i][j] == '.' || st[i][j]) continue;
                st[i][j] = true;
                q[++tt] = {i,j};
            }

    }
}

int main()
{

    #ifdef ONLINE_JUDGE

    #else
    freopen("./in.txt","r",stdin);
    #endif
    ios::sync_with_stdio(false);   
	cin.tie(0);

    scanf("%d%d", &n, &m);

    for(int i = 0; i < n; i++) scanf("%s", g[i]);

    int cnt = 0;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            if(g[i][j] == 'W' && !st[i][j])
            {
                bfs(i, j);
                cnt++;
            }

    cout << cnt << endl;
    return 0;
}